package com.fzy.core.tags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.fzy.core.entity.Page;
import com.fzy.core.utils.BooleanUtils;
import com.fzy.core.utils.StringUtils;
import com.opensymphony.xwork.ActionContext;

public class ExtGridHeadCellTag extends BodyTagSupport {

	private static final long serialVersionUID = -949653217908713132L;
	private static final String DEFAULT_ALIGN = "left";
	
	String page;
	String name;
	String align;
	String sortable;
	
	private String bodyHtml;

	public int doStartTag() throws JspException {
		bodyHtml = "";
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		BodyContent bodycontent = getBodyContent();
		bodyHtml = bodycontent.getString();
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		boolean booleanSortable = BooleanUtils.toBoolean(sortable, false);
		align = StringUtils.defaultIfEmpty(align, DEFAULT_ALIGN);
		
		String sortClass = "x-column-header-sort-null";
		if(booleanSortable){
			String pageName = this.page;
			Page p = (Page)ActionContext.getContext().getValueStack().findValue(pageName);
			if(p != null && p.getOrderBy() != null && p.getOrder() != null && p.getOrderBy().equalsIgnoreCase(name)){
				if(Page.ASC.equalsIgnoreCase(p.getOrder()))
					sortClass = "x-column-header-sort-ASC";
				if(Page.DESC.equalsIgnoreCase(p.getOrder()))
					sortClass = "x-column-header-sort-DESC";
			}
		}
		
		String html = "";
		html += "						<div";
		html += "							style=\"border-width: 1px; width: 100%; height: 100%; position: relative !important;\"";
		html += "							class=\"x-unselectable x-column-header-align-"+align+" x-box-item x-column-header x-unselectable-default x-column-header-sort-undefined "+sortClass+"\">";
		html += "							<div class=\"x-column-header-inner\""+(booleanSortable ? " onclick=\"javascript:sort('"+name+"',this);\"" : "");
		html += "								style=\"width: 100%; height: auto; padding-top: 3px;\">";
		html += "<table cellpadding=\"0\" border=\"0\" cellspace=\"0\" width=\"100%\" height=\"100%\">";
		html += "	<tr>";
		html += "		<td vlign=\"middle\">";
		html += "								<span class=\"x-column-header-text\" style=\"display: block; height: 100%;\">"+ bodyHtml +"</span>";
		html += "		</td>";
		html += "	</tr>";
		html += "</table>";
		if(booleanSortable) html += "								<div id=\"sort_order_by_"+name+"\" class=\"x-column-header-trigger\"></div>";
		html += "							</div>";
		html += "							<div class=\"x-clear\"></div>";
		html += "						</div>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getSortable() {
		return sortable;
	}

	public void setSortable(String sortable) {
		this.sortable = sortable;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

}
